package logo.sys;

import logo.lang.*;

import java.awt.*;
import java.io.*;
import java.util.Scanner;
import javax.swing.*;

/** Main entry point for Logo interpreter. */
public class Logo {

	static final String PROMPT = "Logo> ";

	static final int WIN_SIZE = 650;

	public static JFrame makeWindow(int width, int height) {
		JFrame frame = new JFrame();
		frame.setSize(width, height);
		frame.getContentPane().setLayout(new GridLayout(1, 1));
		frame.setBackground(Color.cyan);
		frame.setVisible(true);
		return frame;
	}

	public static void main(String[] args) throws Exception {
		
		//Definition
		LogoParser parser = null;
		LogoLexer myScanner = null;
		java_cup.runtime.Symbol result = null;
		IRProgram root = null;

		// Create window in which to display turtle display.
		JFrame frame = makeWindow(WIN_SIZE, WIN_SIZE);

		LogoState state = new LogoState();
		TurtleDisplay disp = state.getTurtle().getDisplay();
		frame.getContentPane().add(disp);
		frame.validate();
		disp.setSize(WIN_SIZE, WIN_SIZE);

		while (true) {

			try {

				System.out.print(PROMPT);
				
				//read user input
				Scanner scan = new Scanner(System.in);
				String temp = scan.nextLine();

				//Wraps the user text inside a String reader, appending an EOF to the end
				StringReader reader = new StringReader(temp);
				
				//The reader is then passed to the lexer
				myScanner =  new LogoLexer(reader);
				
				//the lexer passed to the parser				
				parser = new LogoParser(myScanner);
				// System.out.println("Something ?" +
				// parser.debug_parse().toString());

				// while (parser.debug_parse().sym != sym.EOF) {
				// System.out.println("You Rock!!!!");
				// }

				LogoEvaluator eval = new LogoEvaluator();
				// System.out.println("Yes turtle, yes!!!");

				
				/** use for debugging parser*/
				//result = parser.debug_parse();
				
				result = parser.parse();
				root = (IRProgram) result.value;

				//System.out.println("---------------------------");
				
				//walk the tree
				root.visit(eval, state);

				/** my tester */
				// Scanner scan = new Scanner(System.in);
				// Double val = scan.nextDouble();
				// state.getTurtle().forward(val);

			//} catch (Exception e) {
			//	System.err.println("error: " + e.getMessage());
			//}
			}
			catch (LogoException e) {
				System.err.println("error: " + e.getMessage());
				//e.printStackTrace();
			}
			
		}

	}

}
